package com.cloudservice.ccs.controller;

import com.cloudservice.ccs.biz.ISessionMessageBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.model.PageData;
import com.cloudservice.ccs.entity.SessionMessage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/session-message")
public class SessionMessageController {

    @Autowired
    private ISessionMessageBiz sessionMessageBiz;

    @PostMapping("/")
    public ApiResult save(@RequestBody SessionMessage sessionMessage) {
        sessionMessageBiz.save(sessionMessage);
        return ApiResult.success();
    }

    @PutMapping("/{id}")
    public ApiResult update(@PathVariable Integer id, @RequestBody SessionMessage sessionMessage) {
        sessionMessageBiz.update(id, sessionMessage);
        SessionMessage result = sessionMessageBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    // @RequiresPermissions("all")
    @PutMapping("/{id}/partial")
    public ApiResult partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields) {
        sessionMessageBiz.partialUpdate(id, fields);
        SessionMessage result = sessionMessageBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @DeleteMapping("/{id}")
    public ApiResult delete(@PathVariable Integer id) {
        sessionMessageBiz.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/{id}")
    public ApiResult queryById(@PathVariable Integer id) {
        SessionMessage result = sessionMessageBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @GetMapping("/")
    public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<SessionMessage> result = sessionMessageBiz.getAll();
        return ApiResult.successWithData(PageData.fromPage((Page<SessionMessage>) result));
    }

    @GetMapping("/in/{sessionId}")
    public ApiResult queryAllInSession(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer sessionId) {
        PageHelper.startPage(page, limit);
        List<SessionMessage> result = sessionMessageBiz.getAllInSession(sessionId);
        return ApiResult.successWithData(PageData.fromPage((Page<SessionMessage>) result));
    }

    @GetMapping("/in/{sessionId}/before/{id}")
    public ApiResult queryPreviousInSession(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer sessionId, @PathVariable Integer id) {
        PageHelper.startPage(page, limit);
        List<SessionMessage> result = sessionMessageBiz.getPreviousInSession(sessionId, id);
        return ApiResult.successWithData(PageData.fromPage((Page<SessionMessage>) result));
    }
}
